###############################################################################   
#### Replication Materials                                                 #### 
#### Kim, Nakka, Gopal, Desmrais, Mancinelli, Harden, Ko, Boehmke. 2021.   ####
#### Attention to the COVID-19 pandemic on Twitter:                        ####
#### Partisan differences among U.S. state legislators                     ####
#### Legislative Studies Quarterly                                         ####
###############################################################################  


###############################################################################
################################### Set Up ####################################
###############################################################################

# packages -------------------------

lapply(c('readr', 'stargazer', 'texreg', 'xtable', 'lme4', 'glmmTMB','plm',
  'lmtest', 'interplot','dummies','effects', 'ggthemes','caret', 
  'ggeffects', 'scales','ggrepel','sandwich','reshape2'), 
  require, 
  character.only = TRUE)

# read data set for regression -------------------------

data_path <- '/Users/taegyoon/Google Drive/spap_state/spap_state_attention/data/' 
df <- read_csv(paste0(data_path, "spap_state_attention_regression.csv")) 
df <- subset(df, select = -c(X1))


###############################################################################
################### Table S6: Regression with Proportion DV ###################
###############################################################################

# set the panel structure -------------------------

plm_df <- pdata.frame(x = df, 
                      index = c('user.screen_name', 'week'))

# fit models -------------------------

prop_state <- plm(prop_tweets ~ 
                    state_case_pop + 
                    state_death_pop + 
                    state_covid_policy + 
                    party_new + 
                    state_abbrev + 
                    week_new + week_new_2 + week_new_3,
                  data = plm_df,
                  index = c('user.screen_name', 'week_new'),
                  model = 'random', 
                  effect = 'twoways')

prop_national <- plm(prop_tweets ~ 
                       national_case_pop + 
                       national_death_pop + 
                       state_covid_policy + 
                       party_new + 
                       state_abbrev + 
                       week_new + week_new_2 + week_new_3,                                             
                     data = plm_df,
                     index = c('user.screen_name', 'week_new'),
                     model = 'random', 
                     effect = 'twoways')

prop_all <- plm(prop_tweets ~ 
                  state_case_pop + 
                  state_death_pop +
                  national_case_pop + 
                  national_death_pop +
                  state_covid_policy + 
                  party_new +
                  state_abbrev +
                  week_new + week_new_2 + week_new_3,            
                data = plm_df,
                index = c('user.screen_name', 'week_new'),
                model = 'random', 
                effect = 'twoways')

prop_state_inter <- plm(prop_tweets ~ 
                          party_new*state_case_pop + 
                          party_new*state_death_pop +
                          party_new*state_covid_policy + 
                          state_abbrev +
                          week_new + week_new_2 + week_new_3,            
                        data = plm_df,
                        index = c('user.screen_name', 'week_new'),
                        model = 'random', 
                        effect = 'twoways')

prop_national_inter <- plm(prop_tweets ~ 
                             party_new*national_case_pop + 
                             party_new*national_death_pop +
                             party_new*state_covid_policy + 
                             state_abbrev +
                             week_new + week_new_2 + week_new_3,            
                           data = plm_df,
                           index = c('user.screen_name', 'week_new'),
                           model = 'random', 
                           effect = 'twoways')

prop_all_inter <- plm(prop_tweets ~ 
                        party_new*state_case_pop + 
                        party_new*state_death_pop +
                        party_new*national_case_pop + 
                        party_new*national_death_pop +
                        party_new*state_covid_policy + 
                        state_abbrev +
                        week_new + week_new_2 + week_new_3,            
                      data = plm_df,
                      index = c('user.screen_name', 'week_new'),
                      model = 'random', 
                      effect = 'twoways')

prop_all_inter_ctl <- plm(prop_tweets ~ 
                            party_new*state_case_pop + 
                            party_new*state_death_pop +
                            party_new*national_case_pop + 
                            party_new*national_death_pop +
                            party_new*state_covid_policy + 
                            np_score +
                            major_cham +
                            state_abbrev +
                            week_new + week_new_2 + week_new_3,            
                          data = plm_df,
                          index = c('user.screen_name', 'week_new'),
                          model = 'random', 
                          effect = 'twoways')

# adjust SE for serial correlation -------------------------

prop_state_adj <- coeftest(prop_state, 
                           vcovHC(prop_state, 
                                  method = 'arellano'))

prop_national_adj <- coeftest(prop_national, 
                              vcovHC(prop_national, 
                                     method = 'arellano'))

prop_all_adj <- coeftest(prop_all, 
                         vcovHC(prop_all, 
                                method = 'arellano'))

prop_state_inter_adj <- coeftest(prop_state_inter, 
                                 vcovHC(prop_state_inter, 
                                        method = 'arellano'))

prop_national_inter_adj <- coeftest(prop_national_inter, 
                                    vcovHC(prop_national_inter, 
                                           method = 'arellano'))

prop_all_inter_adj <- coeftest(prop_all_inter, 
                               vcovHC(prop_all_inter, 
                                      method = 'arellano'))

prop_all_inter_ctl_adj <- coeftest(prop_all_inter_ctl, 
                                   vcovHC(prop_all_inter_ctl, 
                                          method = 'arellano'))


###############################################################################
######################## Generate Latex Code : Table S7 #######################
###############################################################################

# get information for the last six rows -------------------------

texreg(
  list(prop_state, prop_national, prop_all,
       prop_state_inter, prop__national_inter, prop_all_inter, 
       prop_all_inter_ctl),
  omit.coef = 'abbrev',
  digits = 5,
  caption = 'Panel Regression Model (State Fixed Effect and Legislator-week Random Effect): 
  Population Normalized + Three-fold Party Variable + Week Polynomial + Proportion DV',
  custom.coef.names = c('(Intercept)',
                        'State New Cases (per 10k)',
                        'State New Deaths (per 10k)',
                        'State COVID-19 Policies',
                        'Other',
                        'Republican',
                        'Week',
                        'Week (quadratic)',
                        'Week (cubic)',
                        'National New Cases (per 10k)',
                        'National New Deaths (per 10k)',
                        'Other * State New Cases (per 10k)',
                        'Republican * State New Cases (per 10k)',
                        'Other * State New Deaths (per 10k)',
                        'Republican * State New Deaths (per 10k)',
                        'Other * State COVID-19 Policies',
                        'Republican * State COVID-19 Policies',
                        'Other * National New Cases (per 10k)',
                        'Republican * National New Cases (per 10k)',
                        'Other * National New Deaths (per 10k)',
                        'Republican * National New Deaths (per 10k)',
                        'Legislator Ideology',
                        'Chamber Majority Status'
                        )
  )

# get information for betas and corresponding standard errors -------------------------

texreg(
  list(prop_state_adj, prop_national_adj, prop_all_adj,
       prop_state_inter_adj, prop_national_inter_adj, prop_all_inter_adj, 
       prop_all_inter_ctl_adj),
  omit.coef = 'abbrev',
  digits = 5,
  caption = 'Panel Regression Model (State Fixed Effect and Legislator-week Random Effect): 
  Population Normalized + Three-fold Party Variable + Week Polynomial + Proportion DV',
  custom.coef.names = c('(Intercept)',
                        'State New Cases (per 10k)',
                        'State New Deaths (per 10k)',
                        'State COVID-19 Policies',
                        'Other',
                        'Republican',
                        'Week',
                        'Week (quadratic)',
                        'Week (cubic)',
                        'National New Cases (per 10k)',
                        'National New Deaths (per 10k)',
                        'Other * State New Cases (per 10k)',
                        'Republican * State New Cases (per 10k)',
                        'Other * State New Deaths (per 10k)',
                        'Republican * State New Deaths (per 10k)',
                        'Other * State COVID-19 Policies',
                        'Republican * State COVID-19 Policies',
                        'Other * National New Cases (per 10k)',
                        'Republican * National New Cases (per 10k)',
                        'Other * National New Deaths (per 10k)',
                        'Republican * National New Deaths (per 10k)',
                        'Legislator Ideology',
                        'Chamber Majority Status'
                        )
  )

